package com.easyui.common;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.easyui.DB.ConnectDB;
import com.easyui.DB.OperateDB;
import com.easyui.bean.HeadNote;
import com.easyui.bean.Student;

public class SelNote {
	public SelNote(){}
	
	/**
	 * 获得发布帖子信息
	 * @param pno
	 * @param orderBy
	 * @param sortName
	 * @return
	 */
	public List<Map> getHeadNote(int pno, String orderBy, String sortName) {
		List<Map> initList = null;
		List<Object> plist = null;
		
System.out.println(orderBy);
System.out.println(sortName);
		
		String sql = "";
		sql = "select * from note where pno = " + pno + " and pid = 0 order by " + sortName + " " + orderBy;
		
System.out.println(sql);
		OperateDB.setParmList(plist);//初始化paramList
		initList = OperateDB.excuteQuery(sql);
		
		return initList;
	}
	
	public List<HeadNote> getSonNote(int rootid, int pid) {
		List<HeadNote> sonList = null;
		List<Map> finalList = null;
		List<Object> plist = null;
		
		String sql = "select * from note where rootid = ? and pid = ? order by ptime asc";
System.out.println(sql);
		plist = new ArrayList<Object>();
		plist.add(rootid);
		plist.add(pid);
		OperateDB.setParmList(plist);//初始化paramList
		finalList = OperateDB.excuteQuery(sql);
		
		if(finalList != null && finalList.size() > 0) {
			sonList = new ArrayList<HeadNote>();
			for(int i=0; i<finalList.size(); i++) {
				Map map = finalList.get(i);
				HeadNote son = new HeadNote();
				son.init(map);
				sonList.add(son);
			}
		}
			
		return sonList;
	}
	
	public List<HeadNote> getSpeHeadNote(List<Map> initList, int pno, int page, int rows, String orderBy, String sortName) {
		List<HeadNote> hnlist = null;
		List<Map> finalList = null;
		List<Object> plist = null;
		
		if(initList != null && initList.size() > 0) {
			int totalRecords = 0;
			int totalPage = 0;
			int pageSize = rows;//rows 一页显示几行
			int pageNo = page;//page 第几页
			
			totalRecords = initList.size();
			totalPage = (totalRecords + pageSize -1)/pageSize;
			if(totalPage < 1) {
				totalPage = 1;
			}
			int startPos = (pageNo -1) * pageSize;
			
			String sql = "select * from note where pno = " + pno + " and pid = 0 order by " + sortName + " " + orderBy + 
			" limit " + startPos + ", " + pageSize;
			
System.out.println(sql);
			OperateDB.setParmList(plist);//初始化paramList
			finalList = OperateDB.excuteQuery(sql);
			
			if(finalList != null && finalList.size() > 0) {
				hnlist = new ArrayList<HeadNote>();
				for(int i=0; i<finalList.size(); i++) {
					Map map = finalList.get(i);
					HeadNote hn = new HeadNote();
					hn.init(map);
					hnlist.add(hn);
				}
			}
		}
		
		return hnlist;
	}
	
	/**
	 * 回复主题帖
	 * @return
	 */
	public boolean addMainReply(String role, String userID, int pno, String ncont, int rootid, int pid, List<Map>attachFile, int replysize) {
		boolean success = true;
		
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit = false;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交
			
			boolean attach = false;//是否有附件
			if(attachFile != null && attachFile.size() > 0) {
				attach = true;
			} else {
				attach = false;
			}
			
			String stime = "";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			stime = sdf.format(new java.util.Date());
			
			//向note插入数据
			int nno = newNote(conn, role, userID, pno, "", ncont, stime, attach, rootid, pid);
			if(nno != -1) {
				if(replysize == 0) {//是否更新叶子节点
					if(!updateNoteLeafTime(conn, rootid, stime, "0")) {//最后更新时间字段
						nno = -1;
					}
				} else {//不更新叶子节点
					if(!updateNoteTime(conn, rootid, stime)) {//最后更新时间字段
						nno = -1;
					}
				}
			}
			
			if(nno != -1) {
				if(attach) {//有附件
					//向attachment表插入数据.
					System.out.println("有附件");
					if(new SelAttach().newAttach(conn, userID, pno, nno, role, "note", attachFile)) {
						success = true;
					} else {
						success = false;
					}
				} else {
					success = true;
				}
			} else {
				success = false;
			}
		} catch (SQLException e) {
			success = false;
			System.out.println("添加回复失败，详情如下：\n");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			success = false;
			System.out.println("数据格式化异常，详情如下：\n");
			e.printStackTrace();
		} finally {
			try {
				if (success) {
					conn.commit();//手动提交
				} else {
					conn.rollback();//回滚
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			OperateDB.close(conn);
		}
		
		System.out.println("add:" + success);
		
		return success;
	}
	
	/**
	 * 回复字帖
	 * 不含附件
	 * @param role
	 * @param userID
	 * @param pno
	 * @param ncont
	 * @param rootid
	 * @param pid
	 * @param sonreply
	 * @return
	 */
	public boolean addSonReply(String role, String userID, int pno, String ncont, int rootid, int pid, int sonreply) {
		boolean success = true;
		
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit = false;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交
			
			String stime = "";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			stime = sdf.format(new java.util.Date());
			
			boolean attach = false;
			//向note插入数据
			int nno = newNote(conn, role, userID, pno, "", ncont, stime, attach, rootid, pid);
			
			if(nno != -1) {
				if(sonreply == 0) {//主题帖有回复即表示主题帖已是飞叶子节点
					if(!updateNoteLeaf(conn, pid, "0")) {//更改叶子节点
						nno = -1;
					}
				} 
				
				if(!updateNoteTime(conn, rootid, stime)) {//更改最后更新时间
					nno = -1;
				}
				
				
			}
			
			if(nno != -1) {
				success = true;
			} else {
				success = false;
			}
			
		} catch (SQLException e) {
			success = false;
			System.out.println("添加回复失败，详情如下：\n");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			success = false;
			System.out.println("数据格式化异常，详情如下：\n");
			e.printStackTrace();
		} finally {
			try {
				if (success) {
					conn.commit();//手动提交
				} else {
					conn.rollback();//回滚
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			OperateDB.close(conn);
		}
		
		System.out.println("add:" + success);
		
		return success;
	}
	
	/**
	 * 获得指定id的用户自己发布的帖子
	 * @param userID
	 * @param pno
	 * @param orderBy
	 * @param sortName
	 * @return
	 */
	public List<Map> getOwnHeadNote(String userID, int pno, String orderBy, String sortName) {
		List<Map> initList = null;
		List<Object> plist = null;
		
System.out.println(orderBy);
System.out.println(sortName);
		
		String sql = "";
		sql = "select * from note where pno = " + pno + " and pubno = '" + userID + "' and pid = 0 order by " + sortName + " " + orderBy;
		
System.out.println("own:" + sql);
		OperateDB.setParmList(plist);//初始化paramList
		initList = OperateDB.excuteQuery(sql);
		
		return initList;
	}
	
	/**
	 * 获得指定id的用户自己发布的帖子
	 * 并实现分页
	 * @param userID
	 * @param initList
	 * @param pno
	 * @param page
	 * @param rows
	 * @param orderBy
	 * @param sortName
	 * @return
	 */
	public List<HeadNote> getOwnSpeHeadNote(String userID, List<Map> initList, int pno, int page, int rows, String orderBy, String sortName) {
		List<HeadNote> hnlist = null;
		List<Map> finalList = null;
		List<Object> plist = null;
		
		if(initList != null && initList.size() > 0) {
			int totalRecords = 0;
			int totalPage = 0;
			int pageSize = rows;//rows 一页显示几行
			int pageNo = page;//page 第几页
			
			totalRecords = initList.size();
			totalPage = (totalRecords + pageSize -1)/pageSize;
			if(totalPage < 1) {
				totalPage = 1;
			}
			int startPos = (pageNo -1) * pageSize;
			
			String sql = "";
			sql = "select * from note where pno = " + pno + " and pubno = '" + userID + "' and pid = 0 order by " + sortName + " " + orderBy + 
			" limit " + startPos + ", " + pageSize;
			
System.out.println("own:" + sql);
			OperateDB.setParmList(plist);//初始化paramList
			finalList = OperateDB.excuteQuery(sql);
			
			if(finalList != null && finalList.size() > 0) {
				hnlist = new ArrayList<HeadNote>();
				for(int i=0; i<finalList.size(); i++) {
					Map map = finalList.get(i);
					HeadNote hn = new HeadNote();
					hn.init(map);
					hnlist.add(hn);
				}
			}
		}
		
		return hnlist;
	}
	
	/**
	 * 获得主帖的回复帖子
	 * @param nno
	 * @return
	 */
	public List<Map> getReplyList(int nno) {
		List<Map> initList = null;
		List<Object> plist = null;
		
		String sql = "select * from note where rootid = ? and pid = ? order by ptime asc";
System.out.println(sql);
		plist = new ArrayList<Object>();	
		plist.add(nno);
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
			
		initList = OperateDB.excuteQuery(sql);
			
		return initList;
	}
	
	/**
	 * 发布新的帖子
	 * @param role
	 * @param userID
	 * @param ntitle
	 * @param ncont
	 * @param attachFile
	 * @return
	 */
	public boolean addHeadNode(String role, String userID, int pno, String ntitle, String ncont, List<Map> attachFile) {
		boolean success = true;
		
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit = false;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交
			
			boolean attach = false;//是否有附件
			if(attachFile != null && attachFile.size() > 0) {
				attach = true;
			} else {
				attach = false;
			}
			
			String stime = "";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			stime = sdf.format(new java.util.Date());
			//向note插入数据
			int nno = newNote(conn, role, userID, pno, ntitle, ncont, stime, attach, 0, 0);
			if(nno != -1) {
				System.out.println(nno);
				if(!updateNote(conn, nno)) {//将刚才插入数据库的note的rootid改为正确的
					nno = -1;
				}
			}
			
			if(nno != -1) {
				if(attach) {//有附件
					//向attachment表插入数据.
					System.out.println("有附件");
					if(new SelAttach().newAttach(conn, userID, pno, nno, role, "note", attachFile)) {
						success = true;
					} else {
						success = false;
					}
				} else {
					success = true;
				}
			} else {
				success = false;
			}
			
		} catch (SQLException e) {
			success = false;
			System.out.println("发布帖子失败，详情如下：\n");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			success = false;
			System.out.println("数据格式化异常，详情如下：\n");
			e.printStackTrace();
		} finally {
			try {
				if (success) {
					conn.commit();//手动提交
				} else {
					conn.rollback();//回滚
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			OperateDB.close(conn);
		}
		
		System.out.println("add:" + success);
		
		return success;
	}
	
	/**
	 * 向note表插入数据 
	 * @param conn
	 * @param role
	 * @param userID
	 * @param pno
	 * @param ntitle
	 * @param ncont
	 * @param stime
	 * @param attach
	 * @param rootid
	 * @param pid
	 * @return
	 */
	public int newNote(Connection conn, String role, String userID, int pno, String ntitle, String ncont, String stime, boolean attach, int rootid, int pid) {
		int nno = -1;
		
		List<Object> plist = null;
		Map<String, Object> m = null;
		
		String isAttach = "";
		
		if(attach) {
			isAttach = "1";
		} else {
			isAttach = "0";
		}
		
		String sql = "insert into note values(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		plist = new ArrayList<Object>();
		plist.add(ntitle);
		plist.add(ncont);
		plist.add(stime);
		plist.add(stime);
		plist.add(role);
		plist.add(userID);
		plist.add(pno);
		plist.add(isAttach);
		plist.add(rootid);//rootid,暂时设为1
		plist.add(pid);//pid
		plist.add("1");//是叶子节点
		OperateDB.setParmList(plist);
		
		m = OperateDB.insertAndGet(conn, sql);
		if(m != null && m.size() > 0) {
			boolean success = (Boolean)m.get("isSuccess");
			nno = (Integer)m.get("generatedKey");
		} else {
			nno = -1;
		}
		
		return nno;
	}
	
	/**
	 * 更改note的rootid
	 * @param nno
	 * @return
	 */
	public boolean updateNote(Connection conn, int nno) {
		System.out.println(nno);
		List<Object> plist = null;
		boolean success = false;
		
		String	sql = "update note set rootid = ? where nno = ?";
System.out.println(sql);
		plist = new ArrayList<Object>();	
		plist.add(nno);
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
		
		success = OperateDB.update(conn, sql);
			
		return success;
	}
	
	/**
	 * 更新帖子为非叶子节点
	 * @param conn
	 * @param nno
	 * @return
	 */
	public boolean updateNoteLeafTime(Connection conn, int nno, String ltime, String isleaf) {
		System.out.println(nno);
		List<Object> plist = null;
		boolean success = false;
		
		String	sql = "update note set isleaf = ?, ltime = ? where nno = ?";
System.out.println(sql);
		plist = new ArrayList<Object>();	
		plist.add(isleaf);
		plist.add(ltime);
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
		
		success = OperateDB.update(conn, sql);
			
		return success;
	}
	
	/**
	 * 更新时间
	 * @param conn
	 * @param nno
	 * @param ltime
	 * @return
	 */
	public boolean updateNoteTime(Connection conn, int nno, String ltime) {
		System.out.println(nno);
		List<Object> plist = null;
		boolean success = false;
		
		String	sql = "update note set ltime = ? where nno = ?";
System.out.println(sql);
		plist = new ArrayList<Object>();	
		plist.add(ltime);
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
		
		success = OperateDB.update(conn, sql);
			
		return success;
	}
	
	/**
	 * 更新叶子节点
	 * @param conn
	 * @param nno
	 * @param isleaf
	 * @return
	 */
	public boolean updateNoteLeaf(Connection conn, int nno, String isleaf) {
		System.out.println(nno);
		List<Object> plist = null;
		boolean success = false;
		
		String	sql = "update note set isleaf = ? where nno = ?";
System.out.println(sql);
		plist = new ArrayList<Object>();	
		plist.add(isleaf);
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
		
		success = OperateDB.update(conn, sql);
			
		return success;
	}
	
	/**
	 * 指定id的父贴是否是叶子节点
	 * @param conn
	 * @param pid
	 * @return
	 */
	public boolean isNoteLeaf(Connection conn, int pid) {
		List<Object> plist = null;
		boolean isLeaf = false;
		
		String	sql = "select * from note where pid = ?";
System.out.println("isleaf" + sql + ":" + pid);
		plist = new ArrayList<Object>();	
		plist.add(pid);
		OperateDB.setParmList(plist);//初始化paramList
		
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.excuteQuery(conn, sql);
		
		if(list != null && list.size() > 0) {
			isLeaf = false;
		} else {
			isLeaf = true;
		}
			
		return isLeaf;
	}
	
	public boolean canDelNote(int nno) {
		List<Object> plist = null;
		boolean canDel = false;
		
		String sql = "select * from note where pid = ?";
		plist = new ArrayList<Object>();	
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
		
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.excuteQuery(sql);
		
		if(list != null && list.size() > 0) {
			canDel = false;
		} else {
			canDel = true;
		}
			
		return canDel;
		
	}
	
	public boolean deleteNote(int nno, int pid) {
		List<Object> plist = null;
		boolean success = true;
		
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit = false;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交
			
			success = delNoteAndAttach(conn, nno);
		} catch (SQLException e) {
			success = false;
			System.out.println("删除帖子失败，详情如下：\n");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			success = false;
			System.out.println("数据格式化异常，详情如下：\n");
			e.printStackTrace();
		} finally {
			try {
				if (success) {
					conn.commit();//手动提交
				} else {
					conn.rollback();//回滚
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			OperateDB.close(conn);
		}

		return success;
	}
	
	public boolean delHeadNote(String[] id) {
		List<Object> plist = null;
		boolean success = true;
		
		if (id.length < 1) {
			success = false;
		} else {
			Connection conn = ConnectDB.getConnection();
			boolean autoCommit = false;
			try {
				autoCommit = conn.getAutoCommit();
				conn.setAutoCommit(false);// 取消自动提交

				for (int i = 0; i < id.length; i++) {
					int nno = Integer.parseInt(id[i]);
System.out.println(nno);
					if(!delHeadNote(conn, nno)) {//删除主题帖以及回复贴
						success = false;
						break;
					}
				}

			} catch (SQLException e) {
				success = false;
				System.out.println("删除数据失败，详情如下：\n");
				e.printStackTrace();
			} catch (NumberFormatException e) {
				success = false;
				System.out.println("数据格式化异常，详情如下：\n");
				e.printStackTrace();
			} finally {
				try {
					if (success) {
						conn.commit();//手动提交
					} else {
						conn.rollback();//回滚
					}
					conn.setAutoCommit(autoCommit);// 恢复现场
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				OperateDB.close(conn);
			}
		}

		return success;
	}
	
	/**
	 * 删除指定id的主题帖及回复贴以及各附件
	 * @param conn
	 * @param id
	 * @return
	 */
	public boolean delHeadNote(Connection conn, int id) {
		List<Object> plist = null;
		boolean success = true;
		
		//查询是否有回帖
		String sql = "select * from note where rootid = ?";
		plist = new ArrayList<Object>();	
		plist.add(id);
		OperateDB.setParmList(plist);//初始化paramList
		
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.excuteQuery(conn, sql);
		
		if (list != null && list.size() > 0) {// 存在回帖
			for(int i=0; i<list.size(); i++) {
				Map<String, Object> map = list.get(i);
				int nno = (Integer)map.get("nno");
				if(!delNoteAndAttach(conn, nno)) {
					success = false;
					break;
				}
			}
		} else {//不存在,直接删除主题帖
			if(!delNoteAndAttach(conn, id)) {
				success = false;
			} else {
				success = true;
			}
		}
		
		return success;
	}
	
	/**
	 * 更新父贴为叶子节点
	 * @param pid
	 * @return
	 */
	public boolean updatePNoteLeaf(int pid) {
		List<Object> plist = null;
		boolean success = true;
		
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit = false;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交
			
			if(isNoteLeaf(conn, pid)) {	//删除回帖之后是父贴是叶子节点
				success = updateNoteLeaf(conn, pid, "1");
			}
		} catch (SQLException e) {
			success = false;
			System.out.println("删除帖子失败，详情如下：\n");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			success = false;
			System.out.println("数据格式化异常，详情如下：\n");
			e.printStackTrace();
		} finally {
			try {
				if (success) {
					conn.commit();//手动提交
				} else {
					conn.rollback();//回滚
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			OperateDB.close(conn);
		}

		return success;
	}
	
	/**
	 * 删除指定id的note以及附件
	 * @param conn
	 * @param nno
	 * @return
	 */
	public boolean delNoteAndAttach(Connection conn, int nno) {
		List<Object> plist = null;
		boolean success = true;
		
		String sql ="";
		
		//查询是否有附件
		sql = "select * from note where nno = ?";
		plist = new ArrayList<Object>();	
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
		
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.excuteQuery(conn, sql);
		
		if (list.size() > 0) {// 存在帖子
			Map<String, Object> map = list.get(0);
			String isAppend = map.get("isappend").toString();
	
			if (isAppend.equals("1")) {// 有附件,则删除附件
				sql = "delete from attachment where btype = ? and bno = ?";
				
				plist = new ArrayList<Object>();	
				plist.add("note");
				plist.add(nno);
				OperateDB.setParmList(plist);//初始化paramList
				if(!OperateDB.update(conn, sql)) {//删除附件失败
					success = false;
				}
			}
	
			// 删除note中的信息
			if (success) {
				sql = "delete from note where nno = ?";
				
				plist = new ArrayList<Object>();	
				plist.add(nno);
				OperateDB.setParmList(plist);//初始化paramList
				
				if(!OperateDB.update(conn, sql)) {
					success = false;
				} else {
					success = true;
				}
			}

		} else {
			success = false;
		}
		
		return success;
	}
	
	/**
	 * 获得指定帖的回复贴
	 * 并分页
	 * @param nno
	 * @param page
	 * @return
	 */
	public List<Object> getNote(int nno, int page) {
		List<Map> initList = null;
		List<Object> rlist = null;
		
		initList = getReplyList(nno);
		rlist = getSpeMainNote(initList, nno, page);
		return rlist;
	}
	
	/**
	 * 获得主题帖
	 * @param nno
	 * @return
	 */
	public List<HeadNote> getNote(int nno) {
		List<Object> plist = null;
		List<Map> initList = null;
		List<HeadNote> hnList = null;
		
		
		String sql = "select * from note where nno = ?";
		
		plist = new ArrayList<Object>();
		plist.add(nno);
		OperateDB.setParmList(plist);//初始化paramList
		initList = OperateDB.excuteQuery(sql);
		
		if(initList != null && initList.size() > 0) {
			hnList = new ArrayList<HeadNote>();
			Map<String, Object> map = initList.get(0);
			HeadNote hn = new HeadNote();
			hn.init(map);
			hn.initAttach(map);
			hnList.add(hn);
		}
		return hnList;
	}
	
	/**
	 * 获得回复贴
	 * 并实现分页效果
	 * @param initList
	 * @param nno
	 * @param page
	 * @return
	 */
	public List<Object> getSpeMainNote(List<Map> initList, int nno, int page) {
		List<Object> plist = null;
		List<Map> finalList = null;
		List<Object> rlist = null;
		
		int totalRecords = 0;
		int totalPage = 0;
		int pageSize = 3;//rows 一页显示几行
		int pageNo = page;//page 第几页
		List<HeadNote> hnList = null;
		Map<String, Integer> noteNum = null;
		
		if(initList != null && initList.size() > 0) {
			totalRecords = initList.size();
			totalPage = (totalRecords + pageSize -1)/pageSize;
			if(totalPage < 1) {
				totalPage = 1;
			}
			int startPos = (pageNo -1) * pageSize;
			
			String sql = "select * from note where rootid = " + nno + " and pid = " + nno + " order by ptime" + 
						" limit " + startPos + ", " + pageSize;
			
System.out.println(sql);
			OperateDB.setParmList(plist);//初始化paramList
			finalList = OperateDB.excuteQuery(sql);
			
			rlist = new ArrayList<Object>();
			//存放有关数据
			noteNum = new HashMap<String, Integer>();
			noteNum.put("total", totalPage);//一共多少页
			noteNum.put("current", page);//当前页
			noteNum.put("pagesize", pageSize);//
			rlist.add(noteNum);
			
			if(finalList != null && finalList.size() > 0) {
				//回复帖的数据
				hnList = new ArrayList<HeadNote>();
				for(int i=0; i<finalList.size(); i++) {
					Map map = finalList.get(i);
					HeadNote hn = new HeadNote();
					hn.init(map);
					hn.initAttach(map);
					hnList.add(hn);
				}
				
				rlist.add(hnList);
			} else {
				rlist.add(hnList);
			}
		} else {
			rlist = new ArrayList<Object>();
			//存放有关数据
			noteNum = new HashMap<String, Integer>();
			noteNum.put("total", 1);//一共多少页
			noteNum.put("current", 1);//当前页
			noteNum.put("pagesize", pageSize);//
			rlist.add(noteNum);
			rlist.add(hnList);
		}
		
		if(rlist == null) {
			System.out.println("null");
		} else {
			System.out.println("not null");
		}
		return rlist;
	}
}
